package cn.yunhe.service;

import cn.yunhe.dao.IUserDao;
import cn.yunhe.entity.User;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

public class IUserService {


    /**
     * 用户注册
     * @param user
     * @return
     */
    public  boolean inserUser(User user){
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        IUserDao iUserDao = sqlSession.getMapper(IUserDao.class);
        try{
            int num = iUserDao.inserUser(user);
            sqlSession.commit();
            if (num!=0){
                return true;
            }else {
                return  false;
            }
        }finally {

            sqlSession.close();
        }

    }

    /**
     * 注册的时候判断是否有相同的用户名
     * @param userName
     * @return
     */
    public  boolean setUserName(String userName){
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        IUserDao iUserDao = sqlSession.getMapper(IUserDao.class);
        User user =   iUserDao.setUserName(userName);
        if (user!=null){
            return  true;
        }else {
            return false;
        }
    }

    /**
     * 用户登陆
     * @param user
     * @return
     */
    public  User userLadmin(User user){
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        IUserDao iUserDao = sqlSession.getMapper(IUserDao.class);
        try {
            return  iUserDao.userLading(user);
        }finally {
            sqlSession.close();
        }

    }

}
